package com.loto.insurance.mis.web.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.stereotype.Service;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.DefaultTransactionDefinition;

import com.loto.insurance.mis.web.db.mapper.OrderFlowMapper;
import com.loto.insurance.mis.web.db.mapper.OrderInfoMapper;
import com.loto.insurance.mis.web.db.mapper.PolicyFlowMapper;
import com.loto.insurance.mis.web.db.mapper.PolicyInfoAirMapper;
import com.loto.insurance.mis.web.db.mapper.PolicyInfoTravelMapper;
import com.loto.insurance.mis.web.db.mapper.ProductInfoMapper;
import com.loto.insurance.mis.web.entity.OrderInfo;
import com.loto.insurance.mis.web.entity.ProductInfo;
import com.loto.insurance.mis.web.entity.service.OrderAir;
import com.loto.insurance.mis.web.entity.service.OrderTravel;
import com.loto.insurance.mis.web.entity.service.SimpleOrderInfo;
import com.loto.insurance.mis.web.enumeration.InsTypeEnum;
import com.loto.insurance.mis.web.entity.OrderFlow;
import com.loto.insurance.mis.web.entity.PolicyFlow;
import com.loto.insurance.mis.web.entity.PolicyInfoAir;
import com.loto.insurance.mis.web.entity.PolicyInfoTravel;
import com.loto.insurance.mis.web.enumeration.OrderStatusEnum;
import com.loto.insurance.mis.web.enumeration.PolicyStatusEnum;
import com.loto.insurance.mis.web.enumeration.PolicyFeeStatusEnum;

@Service
public class OrderInfoService {

  @Autowired
  OrderInfoMapper orderInfoMapper;

  @Autowired
  ProductInfoMapper productInfoMapper;

  @Autowired
  OrderFlowMapper orderFlowMapper;

  @Autowired
  PolicyFlowMapper policyFlowMapper;

  @Autowired
  PolicyInfoAirMapper policyInfoAirMapper;

  @Autowired
  PolicyInfoTravelMapper policyInfoTravelMapper;

  @Autowired
  DataSourceTransactionManager transactionManager;

  @Autowired
  private DefaultTransactionDefinition def;

  public ArrayList<OrderInfo> findAllOrders(Map<String, Object> params) {
	ArrayList<OrderInfo> orderList = orderInfoMapper.findAllOrdersPager(params);
	return orderList;
  }

  public long findAllOrdersCount(Map<String, Object> params) {
	return orderInfoMapper.findAllOrdersPagerCount(params);
  }

  public ArrayList<String> findInsCompanyByCust(long custId) {
	return orderInfoMapper.findInsCompanyByCust(custId);
  }

  public String createOrderAir(OrderAir orderAir, long custId) {
	long orderId = 0;
	UUID uuid = UUID.randomUUID();
	String uid = uuid.toString().replace("-", "");
	com.loto.insurance.mis.web.entity.service.OrderInfo oemOrderInfo = orderAir
	    .getOrderInfo();
	/*
	 * Map<String, Object> params = new HashMap<String, Object>();
	 * params.put("productId", oemOrderInfo.getOrderProductFid());
	 * params.put("productSettlePrice", oemOrderInfo.getOrderSettleAmount());
	 */
	ProductInfo productInfo = productInfoMapper
	    .findActiveProductInfoById(oemOrderInfo.getOrderProductFid());
	if (productInfo == null)
	  return null;
	TransactionStatus status = transactionManager.getTransaction(def);
	try {
	  // 创建订单信息
	  OrderInfo orderInfo = new OrderInfo();
	  orderInfo.setOrderAmount(oemOrderInfo.getOrderAmount());
	  orderInfo.setOrderCreatedOn(oemOrderInfo.getOrderCreatedOn());
	  orderInfo.setOrderCustFid(custId);
	  orderInfo.setOrderNo(oemOrderInfo.getOrderNo());
	  // orderInfo.setOrderPaidOn(oemOrderInfo.getOrderPaidOn());
	  orderInfo.setOrderPersonCount(oemOrderInfo.getOrderPersonCount());
	  orderInfo.setOrderPlatformId(uid);
	  orderInfo.setOrderProductCode(productInfo.getProductCode());
	  orderInfo.setOrderProductFid(oemOrderInfo.getOrderProductFid());
	  orderInfo.setOrderProductInsFid(oemOrderInfo.getOrderProductInsFid());
	  orderInfo.setOrderProductInsName(oemOrderInfo.getOrderProductInsName());
	  orderInfo.setOrderProductName(productInfo.getProductName());
	  orderInfo.setOrderProductType(oemOrderInfo.getOrderProductType());
	  orderInfo.setOrderProductTypeFid(oemOrderInfo.getOrderProductTypeFid());
	  orderInfo.setOrderRealAmount(oemOrderInfo.getOrderRealAmount());
	  orderInfo.setOrderSettleAmount(oemOrderInfo.getOrderSettleAmount());
	  orderInfo.setOrderRealSettleAmount(oemOrderInfo
		  .getOrderRealSettleAmount());
	  orderInfo.setOrderStatusFid(oemOrderInfo.getOrderStatusFid());
	  orderInfo.setOrderSubCustFid(oemOrderInfo.getOrderCustFid());
	  orderInfo.setOrderSubCustName(oemOrderInfo.getOrderCustName());
	  orderInfo.setOrderSubId(oemOrderInfo.getOrderId());
	  orderInfo.setOrderSupName(productInfo.getSupName());
	  // orderInfo.setOrderUpdatedOn(orderUpdatedOn);

	  orderInfoMapper.insertOrder(orderInfo);
	  orderId = orderInfo.getOrderId();

	  // 创建订单流
	  OrderFlow orderFlow = new OrderFlow();
	  orderFlow.setFlowOrderFid(orderId);
	  orderFlow.setFlowOperFid(0);
	  orderFlow.setFlowOrderStatusFid(OrderStatusEnum.UNPAY.getIndex());
	  orderFlow.setFlowOrderStatus(OrderStatusEnum.UNPAY.getName());
	  orderFlow.setFlowMemo(OrderStatusEnum.UNPAY.getDesc());
	  orderFlowMapper.insertOrderFlow(orderFlow);

	  // 创建保单信息
	  for (com.loto.insurance.mis.web.entity.service.PolicyInfoAir policyInfo : orderAir
		  .getPolicyList()) {
		// 准备保单信息
		PolicyInfoAir policyInfoAir = new PolicyInfoAir();
		policyInfoAir.setPolicyCustFid(custId);
		policyInfoAir.setPolicyHolderName(policyInfo.getPolicyHolderName());
		policyInfoAir.setPolicyHolderCredentialType(policyInfo
		    .getPolicyHolderCredentialType());
		policyInfoAir.setPolicyHolderCredentialNo(policyInfo
		    .getPolicyHolderCredentialNo());
		policyInfoAir.setPolicyHolderGender(policyInfo.getPolicyHolderGender());
		policyInfoAir.setPolicyHolderBirth(policyInfo.getPolicyHolderBirth());

		policyInfoAir.setPolicyInsuredName(policyInfo.getPolicyInsuredName());
		policyInfoAir.setPolicyInsuredCredentialType(policyInfo
		    .getPolicyInsuredCredentialType());
		policyInfoAir.setPolicyInsuredCredentialNo(policyInfo
		    .getPolicyInsuredCredentialNo());
		policyInfoAir.setPolicyInsuredGender(policyInfo
		    .getPolicyInsuredGender());
		policyInfoAir.setPolicyInsuredBirth(policyInfo.getPolicyInsuredBirth());
		policyInfoAir.setPolicyInsuredTel(policyInfo.getPolicyInsuredTel());
		policyInfoAir.setPolicyInsuredEmail(policyInfo.getPolicyInsuredEmail());

		policyInfoAir.setPolicyFlightNo(policyInfo.getPolicyFlightNo());
		policyInfoAir.setPolicyBegin(policyInfo.getPolicyBegin());
		policyInfoAir.setPolicyEnd(policyInfo.getPolicyEnd());
		policyInfoAir.setPolicyUnitPrice(policyInfo.getPolicyUnitPrice());
		policyInfoAir.setPolicyRealUnitPrice(policyInfo
		    .getPolicyRealUnitPrice());
		policyInfoAir.setPolicyCount(policyInfo.getPolicyCount());
		policyInfoAir.setPolicyAmount(policyInfo.getPolicyAmount());
		policyInfoAir.setPolicyRealAmount(policyInfo.getPolicyRealAmount());
		policyInfoAir.setPolicySettleAmount(policyInfo.getPolicySettleAmount());
		policyInfoAir.setPolicyStatusFid(PolicyStatusEnum.UNINSURED.getIndex());
		policyInfoAir.setPolicyFeeStatusFid(PolicyFeeStatusEnum.UNPAY
		    .getIndex());
		policyInfoAir.setPolicyOutOrderNo(policyInfo.getPolicyOutOrderNo());
		policyInfoAir.setPolicyOrderFid(orderId);
		policyInfoAir.setPolicySubCustFid(policyInfo.getPolicyCustFid());
		policyInfoAirMapper.insertPolicyInfo(policyInfoAir);
		long policyId = policyInfoAir.getPolicyId();
		if (policyId < 1)
		  throw new RuntimeException();
		// 创建保单流
		PolicyFlow policyFlow = new PolicyFlow();
		policyFlow.setFlowInsTypeFid(InsTypeEnum.AIR.getIndex());
		policyFlow.setFlowPolicyFid(policyId);
		policyFlow.setFlowOperFid(0);
		policyFlow
		    .setFlowPolicyStatusFid(PolicyStatusEnum.UNINSURED.getIndex());
		policyFlow.setFlowPolicyStatus(PolicyStatusEnum.UNINSURED.getName());
		policyFlow.setFlowMemo(PolicyStatusEnum.UNINSURED.getDesc());
		policyFlowMapper.insertPolicyFlow(policyFlow);

	  }
	  transactionManager.commit(status);

	} catch (Exception ex) {
	  ex.printStackTrace();
	  transactionManager.rollback(status);
	  uid = null;
	}
	return uid;
  }

  public String createOrderTravel(OrderTravel orderTravel, long custId) {
	long orderId = 0;
	UUID uuid = UUID.randomUUID();
	String uid = uuid.toString().replace("-", "");
	com.loto.insurance.mis.web.entity.service.OrderInfo oemOrderInfo = orderTravel
	    .getOrderInfo();
	/*
	 * Map<String, Object> params = new HashMap<String, Object>();
	 * params.put("productId", oemOrderInfo.getOrderProductFid());
	 * params.put("productSettlePrice", oemOrderInfo.getOrderSettleAmount());
	 */
	ProductInfo productInfo = productInfoMapper
	    .findActiveProductInfoById(oemOrderInfo.getOrderProductFid());
	if (productInfo == null)
	  return null;
	TransactionStatus status = transactionManager.getTransaction(def);
	try {
	  // 创建订单信息
	  OrderInfo orderInfo = new OrderInfo();
	  orderInfo.setOrderAmount(oemOrderInfo.getOrderAmount());
	  orderInfo.setOrderCreatedOn(oemOrderInfo.getOrderCreatedOn());
	  orderInfo.setOrderCustFid(custId);
	  orderInfo.setOrderNo(oemOrderInfo.getOrderNo());
	  // orderInfo.setOrderPaidOn(oemOrderInfo.getOrderPaidOn());
	  orderInfo.setOrderPersonCount(oemOrderInfo.getOrderPersonCount());
	  orderInfo.setOrderPlatformId(uid);
	  orderInfo.setOrderProductCode(productInfo.getProductCode());
	  orderInfo.setOrderProductFid(oemOrderInfo.getOrderProductFid());
	  orderInfo.setOrderProductInsFid(oemOrderInfo.getOrderProductInsFid());
	  orderInfo.setOrderProductInsName(oemOrderInfo.getOrderProductInsName());
	  orderInfo.setOrderProductName(productInfo.getProductName());
	  orderInfo.setOrderProductType(oemOrderInfo.getOrderProductType());
	  orderInfo.setOrderProductTypeFid(oemOrderInfo.getOrderProductTypeFid());
	  orderInfo.setOrderRealAmount(oemOrderInfo.getOrderRealAmount());
	  orderInfo.setOrderSettleAmount(oemOrderInfo.getOrderSettleAmount());
	  orderInfo.setOrderRealSettleAmount(oemOrderInfo
		  .getOrderRealSettleAmount());
	  orderInfo.setOrderStatusFid(oemOrderInfo.getOrderStatusFid());
	  orderInfo.setOrderSubCustFid(oemOrderInfo.getOrderCustFid());
	  orderInfo.setOrderSubCustName(oemOrderInfo.getOrderCustName());
	  orderInfo.setOrderSubId(oemOrderInfo.getOrderId());
	  orderInfo.setOrderSupName(productInfo.getSupName());
	  // orderInfo.setOrderUpdatedOn(orderUpdatedOn);

	  orderInfoMapper.insertOrder(orderInfo);
	  orderId = orderInfo.getOrderId();

	  // 创建订单流
	  OrderFlow orderFlow = new OrderFlow();
	  orderFlow.setFlowOrderFid(orderId);
	  orderFlow.setFlowOperFid(0);
	  orderFlow.setFlowOrderStatusFid(OrderStatusEnum.UNPAY.getIndex());
	  orderFlow.setFlowOrderStatus(OrderStatusEnum.UNPAY.getName());
	  orderFlow.setFlowMemo(OrderStatusEnum.UNPAY.getDesc());
	  orderFlowMapper.insertOrderFlow(orderFlow);

	  // 创建保单信息
	  for (com.loto.insurance.mis.web.entity.service.PolicyInfoTravel policyInfo : orderTravel
		  .getPolicyList()) {
		// 准备保单信息
		PolicyInfoTravel policyInfoTravel = new PolicyInfoTravel();
		policyInfoTravel.setPolicyCustFid(custId);
		policyInfoTravel.setPolicyHolderName(policyInfo.getPolicyHolderName());
		policyInfoTravel.setPolicyHolderCredentialType(policyInfo
		    .getPolicyHolderCredentialType());
		policyInfoTravel.setPolicyHolderCredentialNo(policyInfo
		    .getPolicyHolderCredentialNo());
		policyInfoTravel.setPolicyHolderGender(policyInfo
		    .getPolicyHolderGender());
		policyInfoTravel
		    .setPolicyHolderBirth(policyInfo.getPolicyHolderBirth());

		policyInfoTravel
		    .setPolicyInsuredName(policyInfo.getPolicyInsuredName());
		policyInfoTravel.setPolicyInsuredCredentialType(policyInfo
		    .getPolicyInsuredCredentialType());
		policyInfoTravel.setPolicyInsuredCredentialNo(policyInfo
		    .getPolicyInsuredCredentialNo());
		policyInfoTravel.setPolicyInsuredGender(policyInfo
		    .getPolicyInsuredGender());
		policyInfoTravel.setPolicyInsuredBirth(policyInfo
		    .getPolicyInsuredBirth());
		policyInfoTravel.setPolicyInsuredTel(policyInfo.getPolicyInsuredTel());
		policyInfoTravel.setPolicyInsuredEmail(policyInfo
		    .getPolicyInsuredEmail());

		policyInfoTravel.setPolicyTravelLine(policyInfo.getPolicyTravelLine());
		policyInfoTravel.setPolicyBegin(policyInfo.getPolicyBegin());
		policyInfoTravel.setPolicyEnd(policyInfo.getPolicyEnd());
		policyInfoTravel.setPolicyUnitPrice(policyInfo.getPolicyUnitPrice());
		policyInfoTravel.setPolicyRealUnitPrice(policyInfo
		    .getPolicyRealUnitPrice());
		policyInfoTravel.setPolicyCount(policyInfo.getPolicyCount());
		policyInfoTravel.setPolicyAmount(policyInfo.getPolicyAmount());
		policyInfoTravel.setPolicyRealAmount(policyInfo.getPolicyRealAmount());
		policyInfoTravel.setPolicySettleAmount(policyInfo
		    .getPolicySettleAmount());
		policyInfoTravel.setPolicyStatusFid(PolicyStatusEnum.UNINSURED
		    .getIndex());
		policyInfoTravel.setPolicyFeeStatusFid(PolicyFeeStatusEnum.UNPAY
		    .getIndex());
		policyInfoTravel.setPolicyOutOrderNo(policyInfo.getPolicyOutOrderNo());
		policyInfoTravel.setPolicyOrderFid(orderId);
		policyInfoTravel.setPolicySubCustFid(policyInfo.getPolicyCustFid());
		policyInfoTravelMapper.insertPolicyInfo(policyInfoTravel);
		long policyId = policyInfoTravel.getPolicyId();
		if (policyId < 1)
		  throw new RuntimeException();
		// 创建保单流
		PolicyFlow policyFlow = new PolicyFlow();
		policyFlow.setFlowInsTypeFid(InsTypeEnum.TRAVEL.getIndex());
		policyFlow.setFlowPolicyFid(policyId);
		policyFlow.setFlowOperFid(0);
		policyFlow
		    .setFlowPolicyStatusFid(PolicyStatusEnum.UNINSURED.getIndex());
		policyFlow.setFlowPolicyStatus(PolicyStatusEnum.UNINSURED.getName());
		policyFlow.setFlowMemo(PolicyStatusEnum.UNINSURED.getDesc());
		policyFlowMapper.insertPolicyFlow(policyFlow);

	  }
	  transactionManager.commit(status);

	} catch (Exception ex) {
	  ex.printStackTrace();
	  transactionManager.rollback(status);
	  uid = null;
	}
	return uid;
  }

  // 取消订单
  public boolean cancelOrder(SimpleOrderInfo orderInfo, long custId) {
	boolean isCanceled = false;
	// 取得订单信息
	Map<String, Object> params = new HashMap<String, Object>();
	params.put("subOrderId", orderInfo.getOrderId());
	params.put("subCustId", orderInfo.getOrderCustFid());
	params.put("custId", custId);
	OrderInfo order = orderInfoMapper.findOrderInfoBySubId(params);
	if (order != null
	    && order.getOrderStatusFid() == OrderStatusEnum.UNPAY.getIndex()) {
	  TransactionStatus status = transactionManager.getTransaction(def);
	  long orderId = order.getOrderId();
	  try {
		// 修改订单状态
		params = new HashMap<String, Object>();
		params.put("orderId", orderId);
		params.put("orderStatusFid", OrderStatusEnum.CANCELED.getIndex());
		orderInfoMapper.updateOrderStatusById(params);

		// 创建订单流
		OrderFlow orderFlow = new OrderFlow();
		orderFlow.setFlowOrderFid(orderId);
		orderFlow.setFlowOperFid(0);
		orderFlow.setFlowOrderStatusFid(OrderStatusEnum.CANCELED.getIndex());
		orderFlow.setFlowOrderStatus(OrderStatusEnum.CANCELED.getName());
		orderFlow.setFlowMemo(OrderStatusEnum.CANCELED.getDesc());
		orderFlowMapper.insertOrderFlow(orderFlow);

		// 修改保单状态为取消
		Map<String, Object> statusParams = new HashMap<String, Object>();
		statusParams.put("orderId", orderId);
		statusParams.put("statusId", PolicyStatusEnum.CANCELED.getIndex());

		Map<String, Object> policyParams = new HashMap<String, Object>();
		policyParams.put("orderId", orderId);
		policyParams.put("flowInsTypeFid", orderInfo.getOrderProductTypeFid());
		policyParams.put("flowPolicyStatusFid",
		    PolicyStatusEnum.CANCELED.getIndex());
		policyParams.put("flowPolicyStatus",
		    PolicyStatusEnum.CANCELED.getName());
		policyParams.put("flowFeeFlowFid", 0);
		policyParams.put("flowMemo", PolicyStatusEnum.CANCELED.getDesc());
		if (orderInfo.getOrderProductTypeFid() == InsTypeEnum.AIR.getIndex()) {
		  policyInfoAirMapper.updatePolicyStatusByOrderId(statusParams);
		  // 批量创建保单流
		  policyFlowMapper.insertAirPolicyFlowBat(policyParams);
		} else if (orderInfo.getOrderProductTypeFid() == InsTypeEnum.TRAVEL
		    .getIndex()) {
		  policyInfoTravelMapper.updatePolicyStatusByOrderId(statusParams);
		  // 批量创建保单流
		  policyFlowMapper.insertTravelPolicyFlowBat(policyParams);
		}
		transactionManager.commit(status);
		isCanceled = true;
	  } catch (Exception ex) {
		ex.printStackTrace();
		transactionManager.rollback(status);
	  }
	}
	return isCanceled;
  }

 
}
